package com.ithaipeng.redanttakeaway.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithaipeng.redanttakeaway.dto.OrdersDto;
import com.ithaipeng.redanttakeaway.exception.CustomException;
import com.ithaipeng.redanttakeaway.pojo.OrderDetail;
import com.ithaipeng.redanttakeaway.pojo.Orders;
import com.ithaipeng.redanttakeaway.service.IOrderDetailService;
import com.ithaipeng.redanttakeaway.service.IOrdersService;
import com.ithaipeng.redanttakeaway.tools.BaseContext;
import com.ithaipeng.redanttakeaway.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IOrderDetailService orderDetailService;
    /**
     * 下单操作!
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return Result.success("下单成功!");
    }
    @GetMapping("/userPage")
    public Result<Page<OrdersDto>> page(@RequestParam(required = false,defaultValue = "1") Integer page,
                                        @RequestParam(required = false,defaultValue = "5") Integer pageSize){
        log.info("{},{}",page,pageSize);
        Page<Orders> ordersPage=new Page<>(page,pageSize);
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper=new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(userId!=null,Orders::getUserId,userId);
        ordersLambdaQueryWrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(ordersPage,ordersLambdaQueryWrapper);
        Page<OrdersDto> ordersDtoPage=new Page<>(page,pageSize);
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        List<Orders> records = ordersPage.getRecords();
        List<OrdersDto> ordersDtoList = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            Long orderId = item.getId();
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(orderId != null, OrderDetail::getOrderId, orderId);
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailLambdaQueryWrapper);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(ordersDtoList);
        return Result.success(ordersDtoPage);
    }
    @GetMapping("/page")
    public Result<Page<Orders>> pageInfo(@RequestParam(required = false,defaultValue = "1") Integer page,
                                         @RequestParam(required = false,defaultValue = "10") Integer pageSize, LocalDateTime orderTime,
                                         String number){
        Page<Orders> ordersPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper=new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(number!=null,Orders::getNumber,number);
        ordersLambdaQueryWrapper.like(orderTime!=null,Orders::getOrderTime,orderTime);
        ordersLambdaQueryWrapper.orderByDesc(Orders::getCheckoutTime).orderByAsc(Orders::getOrderTime);
        ordersService.page(ordersPage,ordersLambdaQueryWrapper);
        List<Orders> ordersList = ordersPage.getRecords();
        if(ordersList.isEmpty()){
            throw new CustomException("暂时还没有客户下单!");
        }
        List<Orders> orders = ordersList.stream().peek((item) ->
                item.setUserName(item.getConsignee())).collect(Collectors.toList());
        ordersPage.setRecords(orders);
        return Result.success(ordersPage);
    }
}
